package com.javaketang.dky.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.javaketang.dky.entity.EasybuyOrder;
import com.javaketang.dky.entity.EasybuyUser;
import com.javaketang.dky.mapper.EasybuyOrderMapper;
import com.javaketang.dky.service.impl.EasybuyOrderServiceImpl;
import org.omg.CORBA.INTERNAL;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author dky
 * @since 2021-07-06
 */
@Controller
@RequestMapping("order")
public class EasybuyOrderController {


    /**
     * 订单类
     */
    @Resource
    private EasybuyOrderMapper orderMapper;

    //订单类
    @Resource
    private EasybuyOrderServiceImpl orderService;

    /**
     * 查询所有订单和我的订单
     *
     * @param userId  用户id
     * @param session session
     * @param model
     * @return
     */
    @RequestMapping("/queryAllOrderOrByUserId")
    public String queryAllOrderOrByUserId(Integer current, Integer userId, HttpSession session, Model model) {

        System.out.println("当前页："+current+"\t用户id:"+userId);

        int pageNumber = 5;

        //分页
        Page<EasybuyOrder> page = new Page<>(current == null ? 1 : current, pageNumber);


        //创建list集合
        List list = orderService.queryByIdOrAllOrder(page, userId);

        //创建list集合
        List list2 = orderService.queryByIdOrAllOrder2(null, userId);


        //查询我的订单
        if (userId != null) {

            //根据用户id查询订单
//           list=orderMapper.selectList(new QueryWrapper<EasybuyOrder>().eq("userId",userId));

            session.setAttribute("action", "queryAllOrder");
            session.setAttribute("show", "queryById");

            //查询所有订单
        } else {

            //根据用户id查询订单
//            list=orderMapper.selectList(null);

            session.setAttribute("action", "queryAllOrder");
            session.setAttribute("show", "queryAllOrder");


        }
        //获取总页数
        int sumDateNumber = (int) (page.getTotal() % pageNumber == 0 ? page.getTotal() / pageNumber : page.getTotal() / pageNumber + 1);


        //赋值订单状态
        model.addAttribute("orderState", list);
        model.addAttribute("orderState2", list2);
        model.addAttribute("sumPage", sumDateNumber);
        model.addAttribute("current", current == null ? 1 : current);
        model.addAttribute("userId", userId);//赋值用户id


        return "Member_User";
    }

    /**
     * 查看单条订单
     *
     * @param id
     * @param session
     * @return
     */
    @RequestMapping("/queryOrderDeatil")
    public String queryOrderDeatil(Integer id, HttpSession session, Model model) {

        session.setAttribute("action", "queryOrderDeatil");

        Object show = session.getAttribute("show");

        System.out.println("=="+show);

        session.setAttribute("show", show);

        //查询单条商品
        List list = orderService.queryOrderById(id);

        model.addAttribute("list", list);

        return "Member_User";
    }


}

